package com.example.ceramics.fragment;

import android.text.Layout;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.viewpager.widget.ViewPager;

import com.example.ceramics.R;
import com.example.ceramics.activity.BlindBoxActivity;
import com.example.ceramics.activity.DetailActivity;
import com.example.ceramics.activity.SearchActivity;
import com.example.ceramics.activity.ServiceActivity;
import com.example.ceramics.activity.SurroundingActivity;
import com.example.ceramics.activity.TestActivity;
import com.example.ceramics.activity.TimeActivity;
import com.example.ceramics.data.Cart;
import com.example.ceramics.data.Datas;
import com.example.ceramics.database.dao.GoodsDaoImpl;
import com.example.ceramics.database.dao.IGoodsDao;
import com.example.ceramics.entity.GoodsEntity;
import com.example.ceramics.util.StringUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageClickListener;
import com.synnapps.carouselview.ImageListener;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class HomeFragment extends BaseFragment {

    private CarouselView carouselView;
    private ImageView aaa;
    private int[] images = {
            R.mipmap.zodiac,
            R.mipmap.five_elements,
            R.mipmap.birds
    };

    private LinearLayout blind_box;
    private LinearLayout surrounding;
    private LinearLayout service;
    private LinearLayout time;
    private ImageView ivSearch;
    private EditText etSearch;

    private IGoodsDao goodsDao;

//    private ViewPager viewPager;
//    private CommonTabLayout commonTabLayout;

    public HomeFragment() {
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        carouselView = mRootView.findViewById(R.id.carouselView);
        aaa = mRootView.findViewById(R.id.aaa);

        blind_box = mRootView.findViewById(R.id.blind_box);
        surrounding = mRootView.findViewById(R.id.surrounding);
        service = mRootView.findViewById(R.id.service);
        time = mRootView.findViewById(R.id.time);
        ivSearch = mRootView.findViewById(R.id.iv_search);
        etSearch = mRootView.findViewById(R.id.et_search);

//        viewPager = mRootView.findViewById(R.id.viewpager);
//        commonTabLayout = mRootView.findViewById(R.id.commonTabLayout);
    }

    @Override
    protected void initData() {
        goodsDao = new GoodsDaoImpl(getActivity());
        carouselView.setImageListener(new ImageListener() {
            @Override
            public void setImageForPosition(int position, ImageView imageView) {
                imageView.setImageResource(images[position]);
            }
        });
        carouselView.setPageCount(images.length);
        carouselView.setImageClickListener(new ImageClickListener() {
            @Override
            public void onClick(int position) {
                GoodsEntity goodsEntity = new GoodsEntity();
                switch (position){
                    case 0:goodsEntity = goodsDao.listGoodsByType("十二生肖系列").get(0);break;
                    case 1:goodsEntity = goodsDao.listGoodsByType("阴阳五行系列").get(0);break;
                    case 2:goodsEntity = goodsDao.listGoodsByType("百鸟朝凤系列").get(0);break;
                }
                navigateTo(DetailActivity.class, "detail", goodsEntity);
            }
        });

//        aaa.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                navigateTo(TestActivity.class);
//            }
//        });

        blind_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                viewPager.setCurrentItem(1);
//                commonTabLayout.setCurrentTab(1);
                navigateTo(BlindBoxActivity.class);
            }
        });
        surrounding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(SurroundingActivity.class);
            }
        });
        service.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(ServiceActivity.class);
            }
        });
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(TimeActivity.class);
            }
        });
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
            }
        });
    }

    private void search() {
        String searchWord = etSearch.getText().toString();
        if (StringUtils.isEmpty(searchWord)) {
            showToast("请输入想要搜索的内容！");
            return;
        }
//        Datas datas = new Datas();
//        datas.setDatas();
//        List<GoodsEntity> searchGoods = datas.search(searchWord);
//        HashMap<String, Object> searchMap = new HashMap<>();
//        searchMap.put("list", searchGoods);
//        searchMap.put("searchWord", searchWord);
        navigateTo(SearchActivity.class, "searchWord", searchWord);
    }
}